package com.divemotor.ventarepuestosinternet.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.divemotor.ventarepuestosinternet.beans.adapter.ModeloAdapter;
import com.divemotor.ventarepuestosinternet.beans.adapter.VehiculoAdapter;
import com.divemotor.ventarepuestosinternet.beans.form.VehiculoBeanForm;
import com.divemotor.ventarepuestosinternet.dao.VehiculoDAO;
import com.divemotor.ventarepuestosinternet.model.Vehiculo;
import com.divemotor.ventarepuestosinternet.util.Constantes;

@SuppressWarnings("unchecked")
@Repository(value = "vehiculoDAO")
public class VehiculoDAOImpl implements VehiculoDAO, Serializable {

	private static final long serialVersionUID = -6565659039930597693L;
	
	@Autowired
	private SessionFactory sessionFactory;
	
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public List<Vehiculo> listar() {
		
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Vehiculo.class, "vehiculo");
		criteria.createAlias("vehiculo.modelo", "modelo");
		criteria.createAlias("vehiculo.propietario", "cliente");
		criteria.add(Restrictions.isNotNull("modelo.modeloPK.idModelo"));
		criteria.add(Restrictions.isNotNull("modelo.modeloPK.idMarca"));
		criteria.add(Restrictions.isNotNull("modelo.modeloPK.idTipoVehiculo"));
		criteria.add(Restrictions.isNotNull("cliente.idCliente"));
		
		return criteria.list();
	}

	@Override
	public List<VehiculoAdapter> listarPorCliente(VehiculoBeanForm vehiculoBean) {		
		
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Vehiculo.class, "vehiculo");
		criteria.createAlias("vehiculo.modelo", "modelo");
		criteria.createAlias("vehiculo.modelo.tipoVehiculo", "tipoVehiculo");
		criteria.createAlias("vehiculo.propietario", "cliente");
		criteria.createAlias("modelo.marca", "marca");
		
		criteria.add(Restrictions.isNotNull("modelo.modeloPK.idModelo"));
		criteria.add(Restrictions.isNotNull("modelo.modeloPK.idMarca"));
		criteria.add(Restrictions.isNotNull("modelo.modeloPK.idTipoVehiculo"));
		criteria.add(Restrictions.isNotNull("cliente.idCliente"));
		criteria.add(Restrictions.eq("cliente.idCliente", vehiculoBean.getIdCliente()));
		
		criteria.setProjection(Projections.projectionList()
				.add(Projections.property("chasis"),"chasis")
				.add(Projections.property("placa"),"placa")
				.add(Projections.property("modelo.nombre"),"modelo")
				.add(Projections.property("marca.nombre"),"marca")
				.add(Projections.property("marca.codigoMarca"),"idMarca")
				.add(Projections.property("tipoVehiculo.nombre"),"tipoVehiculo"));
		
		String porcentaje = Constantes.PORCENTAJE;
		
		String marca = vehiculoBean.getMarca(); 
		if(StringUtils.isNotBlank(marca) && !marca.equals(Constantes.STR_NO_SELECCIONADO)) {
			criteria.add(Restrictions.eq("marca.codigoMarca", marca));
		} 
		
		ModeloAdapter modelo = vehiculoBean.getModeloAdapter();
		if (modelo != null) {
			criteria.add(Restrictions.eq("modelo.modeloPK.idModelo", modelo.getCodigoModelo()));
			criteria.add(Restrictions.eq("modelo.modeloPK.idMarca", modelo.getCodigoMarca()));
			criteria.add(Restrictions.eq("modelo.modeloPK.idTipoVehiculo", modelo.getCodigoTipoVehiculo()));
		}
		
		String placa = vehiculoBean.getPlaca();
		if(StringUtils.isNotBlank(placa) && !placa.equals(Constantes.EMPTY)) {
			criteria.add(Restrictions.like("vehiculo.placa",porcentaje + placa + porcentaje).ignoreCase());
		}
		
		String chasis = vehiculoBean.getChasis();
		if (StringUtils.isNotBlank(chasis) && !chasis.equals(Constantes.EMPTY)) {
			criteria.add(Restrictions.like("vehiculo.chasis",porcentaje + chasis + porcentaje).ignoreCase());
		}
		
		criteria.setResultTransformer(Transformers.aliasToBean(VehiculoAdapter.class));
		
		return criteria.list();	
	}

	@Override
	public Vehiculo obtenerPorId(String chasis) {
		return (Vehiculo) sessionFactory.getCurrentSession().get(Vehiculo.class, chasis);
	}
}
